﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class CamTakeSetInfo
{
	public int[] introTake;
	public int[] idleTake;
	[HideInInspector]
	public TakeInfo[] introTakeinfo;
	public TakeInfo[] idleTakeinfo;
}

public class CamTakeSetting : MonoBehaviour {

	private static CamTakeSetting instance;
	
	public static CamTakeSetting Instance{
		get{
			return instance;
		}
	}
	public GameObject cameraRoot;
	[HideInInspector]
	public float idleCamEnterTimeTmp;
	public float interpolateTime = 4.0f;
	public string introTakePrefix = "camera_intro";
	public string idleTakePrefix = "camera_idle";
	public CamTakeSetInfo [] takeSet;
	public CamTakeSetInfo takeSetForPvp;
	public bool bTestCam;
	public bool bPauseCam;
	public bool bTogglePlay;
	public int nPlayType;  // 0: intro  1: idle
	public int nIdleIndex;
	public float startPlayTime;

	CamTakeSetInfo takeSetToUse;
	IEventlinePlayer camTakePlayer = null;
	[HideInInspector]
	public bool bStartedIdleCamera;
	public bool allowTouchCam;

	LegionTouchCam touchCam;

	void Awake()
	{
		instance = this;
		foreach(CamTakeSetInfo takeSetInfo in takeSet) {
			LoadTakeSetInfo(takeSetInfo);
		}
		LoadTakeSetInfo(takeSetForPvp);

	}

	void Start()
	{
		if(bTestCam) {
			takeSetToUse=takeSet[0];
			PlayIntro ();
		}

	}

	void Update()
	{
		if(bPauseCam) {
			if(camTakePlayer!=null) {
				if(camTakePlayer.IsPaused)
					camTakePlayer.Resume();
				else
					camTakePlayer.Pause();
			}
			bPauseCam=false;
		}
		if(bTogglePlay) {
			if(camTakePlayer!=null) {
				StopIdle();
			}
			else
				PlayIdle();
			bTogglePlay=false;
		}
	}

	void LoadTakeSetInfo(CamTakeSetInfo takeSetInfo)
	{
		if(takeSetInfo.introTake.Length>0)
			takeSetInfo.introTakeinfo = new TakeInfo[takeSetInfo.introTake.Length];
		for(int i=0; i< takeSetInfo.introTake.Length; i++) {
			TakeInfo tinfo = LoadTake( introTakePrefix, takeSetInfo.introTake[i]);
			if(tinfo!=null)
				takeSetInfo.introTakeinfo[i] = tinfo;
			//introTakes.Add(tinfo);
		}
		if(takeSetInfo.idleTake.Length>0)
			takeSetInfo.idleTakeinfo = new TakeInfo[takeSetInfo.idleTake.Length];
		for(int i=0; i< takeSetInfo.idleTake.Length; i++) {
			TakeInfo tinfo = LoadTake( idleTakePrefix, takeSetInfo.idleTake[i]);
			if(tinfo!=null)
				takeSetInfo.idleTakeinfo[i] = tinfo;
			//idleTakes.Add(tinfo);
		}
	}
	
	TakeInfo LoadTake(string prefix, int num)
	{
		string takePath = string.Format("{0}{1:00}",prefix, num);
		EventlineTake newTake = Eventline.LoadTake(takePath);
		if(newTake==null) {
			Debug.LogError("cam take null  "+takePath);
			return null;
		}
		else {
			TakeInfo tinfo = new TakeInfo();
			tinfo.take=newTake;
			//tinfo.length = tinfo.take.TakeLength/(float)tinfo.take.FrameRate;
			return tinfo;
		}
	}


	public void PlayIntroCam(int nWave, bool isPvp)
	{
		if(isPvp) {
			StopIdle();
			takeSetToUse=takeSetForPvp;
			PlayIntro ();
		}
		else {
			if(nWave < takeSet.Length) {
				StopIdle();
				takeSetToUse=takeSet[nWave];
				PlayIntro ();
			}
		}
	}

	void InitCamera()
	{
		if(EventlineStaticActor.Camera==null) {
			if(cameraRoot!=null)
				EventlineStaticActor.Camera = cameraRoot;
			else if(BattleManager.Instance!=null) {
				EventlineStaticActor.Camera = BattleManager.Instance.cameraRoot;
				cameraRoot = BattleManager.Instance.cameraRoot;
			}
		}
		if(cameraRoot==null && BattleManager.Instance!=null)
			cameraRoot = BattleManager.Instance.cameraRoot;

		if(BattleManager.Instance==null)
			allowTouchCam=true;
	}

	void PlayIntro()
	{
		if( takeSetToUse.introTake!=null && takeSetToUse.introTake.Length>0) {
			InitCamera();
			Object[] camActor = {(Object)cameraRoot};
			int nIndex = Random.Range(0, takeSetToUse.introTake.Length);
			nPlayType=0;
			camTakePlayer = Eventline.Play( takeSetToUse.introTakeinfo[nIndex].take, camActor, cameraRoot, enter_time:0); 
			float length = takeSetToUse.introTakeinfo[nIndex].take.TakeLength/(float)takeSetToUse.introTakeinfo[nIndex].take.FrameRate;
			RDebug.Log("Play Intro Cam   "+takeSetToUse.introTake[nIndex]+"   length="+length);
		}
		else
			PlayIdle();
	}

	public void PlayIdle()
	{
		InitCamera();
		if(takeSetToUse.idleTakeinfo==null || takeSetToUse.idleTakeinfo.Length==0)
			return;
		if(bStartedIdleCamera) {
			Debug.LogError("Already play idle camera... return");
			return;
		}
		nPlayType=1;
		bStartedIdleCamera = true;
		Object[] camActor = {(Object)cameraRoot};
		startPlayTime = Time.timeSinceLevelLoad;
		camTakePlayer = Eventline.Play( takeSetToUse.idleTakeinfo[nIdleIndex].take, camActor, cameraRoot, enter_time:idleCamEnterTimeTmp); 
		idleCamEnterTimeTmp = interpolateTime;
		float length = takeSetToUse.idleTakeinfo[nIdleIndex].take.TakeLength/(float)takeSetToUse.idleTakeinfo[nIdleIndex].take.FrameRate;
		RDebug.Log("PlayIdleCam   "+takeSetToUse.idleTake[nIdleIndex]+"    interpol="+idleCamEnterTimeTmp+"   length="+length);
		nIdleIndex++;
		if(nIdleIndex>=takeSetToUse.idleTakeinfo.Length)
			nIdleIndex=0;

	}

	public void StopIdle()
	{
		if(camTakePlayer!=null) {
			camTakePlayer.Stop();
			camTakePlayer=null;
			
			bStartedIdleCamera=false;
			nPlayType=-1;
		}
	}

	public void DeactivateFreeCam()
	{
		if(touchCam==null)
			touchCam = cameraRoot.GetComponent<LegionTouchCam>();
		if(touchCam!=null)
			touchCam.DeactivateFreeCam();
	}

	public void SetBackTakeCamWaitTime(float _setTime)
	{
		if(touchCam==null)
			touchCam = cameraRoot.GetComponent<LegionTouchCam>();
		if(touchCam!=null)
			touchCam.BackTakeCamWaitTime =_setTime;
	}

	public void SetSkillCamera_Old(bool bSet)
	{
		if(bSet) {
			DeactivateFreeCam();
			StopIdle();
			allowTouchCam=false;
		}
		else {
			PlayIdle();
			allowTouchCam=true;
		}
	}

	public void SetSkillCamera(bool bSet)
	{
		if(touchCam==null)
			touchCam = cameraRoot.GetComponent<LegionTouchCam>();
		if(touchCam!=null && touchCam.freecamMode)
		{
			if(bSet) {
				EventlineStaticActor.Camera = null;
				EventlineStaticActor.MainCamera = null;
			}
		}
		else
		{
			if(bSet) {
				EventlineStaticActor.MainCamera.transform.localPosition = Vector3.zero;
				EventlineStaticActor.MainCamera.transform.localRotation = Quaternion.identity;
				StopIdle();
				allowTouchCam=false;
			}
			else {
				PlayIdle();
				allowTouchCam=true;
			}
		}
		if(!bSet) {
			EventlineStaticActor.Camera = BattleManager.Instance.cameraRoot;
			EventlineStaticActor.MainCamera = Camera.main.gameObject;
		}
	}
}
